package com.atguigu.gulimall.order.web;

import com.atguigu.gulimall.order.entity.OrderEntity;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

@Controller
public class HelloController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("{page}.html")
    public String toDetailPage(@PathVariable("page")String page) {
        return page;
    }


    /**
     * 测试用: 发送消息
     *  给RabbitMQ发送消息
     * @return
     */
    @ResponseBody
    @GetMapping("/test/createOrder")
    public String createOrderTest(){
        //1、模拟创建Order订单
        OrderEntity entity = new OrderEntity();
        entity.setCreateTime(new Date());
        entity.setOrderSn(UUID.randomUUID().toString().replace("-",""));
        //2、给MQ发送消息
        /**
         * 参数1：指定交换器
         * 参数2：指定路由键
         * 参数3：指定消息本身（任意类型都可以，该方法会自动序列化。如果是个对象，注意要实现序列化接口。）
         * 参数4：一些参数(无)
         */
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",entity);
        return "ok";
    }


}
